﻿@model List<Users>
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <script src="~/lib/jquery/dist/jquery.js"></script>
    <script type="text/javascript">
        $(function() {
            $("#subtest").click(function() {
                $(".orderresult").each(function(i, item) {
                    var userid = $(item).data("uid");
                    $.ajax({
                        type: 'POST',
                        url: "/home/Buy",
                        data: { userId: userid },
                        beforeSend: function() {
                            $(item).html("开始抢购");
                        },
                        success: function (result) {
                            switch (result.code) {
                            case 1:
                                $(item).html("订单下单成功,5秒后跳转");
                                //等待5秒查询结果
                                setTimeout(function() {
                                    checkOrder(item, userid);
                                }, 5000);
                                    break;
                                case -1:
                                    $(item).html("抢购失败,请重试!");
                                break;
                            case 0:
                                $(item).html("商品已售罄!");
                                break;
                            case -2:
                                $(item).html("系统异常!");
                                break;
                            }
                        }
                    });
                });
            });
        });
        function checkOrder(obj,userid) {
            $.ajax({
                type: 'POST',
                url: "/home/GetBuyResult",
                data: { userId: userid },
                success: function (result) {
                    $(obj).html(""+JSON.stringify(result));
                }
            });
        }
    </script>
</head>
<body>
<input type="button" value="test" id="subtest" />
<div>
    <p>result:</p>
    <div id="ajaxresult">
        @foreach (Users t in Model.ToList())
        {
            @Html.Raw($"<div>姓名:{t.UserName},购买结果:<span class=\"orderresult\" data-uid=\"{t.ID}\">等待购买</span></div>")
            //@Html.Raw($"<input type=\"hidden\" class=\"orderresult\" data-uid=\"{t.ID}\"/>")
        }
    </div>
</div>
</body>
</html>
